# -*- coding: utf-8 -*-
import scrapy


class GithubSpider(scrapy.Spider):
    name = 'github'
    allowed_domains = ['github.com']
    start_urls = ['https://github.com/L-huihui']

    # 重写start_requests方法
    def start_requests(self):
        # 通过抓包获取cookies
        cookies_str = 'has_recent_activity=1; _ga=GA1.2.868520070.1551358475; _gat=1; tz=Asia%2FShanghai; ' \
                      '_octo=GH1.1.866882906.1551358533; ' \
                      'user_session=AM1E2dgzVG9DTYH7Iq7PTzfQzFHoU617gjyEgSDLs4-OxYDw; ' \
                      '__Host-user_session_same_site=AM1E2dgzVG9DTYH7Iq7PTzfQzFHoU617gjyEgSDLs4-OxYDw; logged_in=yes; ' \
                      'dotcom_user=L-huihui; ' \
                      '_gh_sess=ZnZXZGpveFFhejcvRUVRdUdTTDZ1eWU4eVIxcDVIMndQWTVpNEo1MGNvWGIxTVhFL2YzODhZM0hNalB2OHhkSkoycnRVUjVHaldjRUJFNjI1bG52cm5RUlh3czVLd3lLQnJCemNZQktKRXJTeTZ1dUFyc3pCaXo2clhHYlp4clZpVFovbmFseENZMDU0UzFZUGNpa2pFTjh6cXowMEFtRVNlSi9HYkhkRWN1d0ZUQ1lGRmozdG5zTTBkd0xMdDRILS04T1QzVE1RTjRodjZBcW9NZzJtaXBRPT0%3D--f71a6672a19c748a00901a3e082280d9042740c3 '
        # 将cookies_str转换为cookie_dict
        cookies_dict = {i.split('=')[0]:i.split('=')[-1] for i in cookies_str.split('; ')}
        yield scrapy.Request(
            self.start_urls[0],
            callback=self.parse,
            cookies=cookies_dict
        )

    def parse(self, response):
        print(response.body)
        print(response.url)
        pass
